﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Streaming_Service.SQL_Ting {
    using System.Data;
    using System.Data.SqlClient;

    public class DBHandler {
        private const string ConnectionString = "Data Source=rentit.itu.dk;Initial Catalog=RENTIT11;User Id=rentit11db;Password=Zaq12wsx;";

        public static SqlHandler GetHandler() {
            return new SqlHandler("rentit.itu.dk", "rentit11db", "Zaq12wsx", "RENTIT11");
        }
    }

    public class SqlHandler {
        public string Host { get; private set; }
        public string Username { get; private set; }
        public string Password { get; private set; }
        public string Database { get; private set; }

        public bool Connected { get; private set; }

        private SqlConnection Connection { get; set; }

        private string ConnectionString {
            get {
                return "Data Source=" + Host + ";User Id=" + Username + ";Initial Catalog=" + Database + ";Password=" + Password + ";";
            }
        }

        public SqlHandler(string host, string username, string password, string database) {
            this.Host = host;
            this.Username = username;
            this.Password = password;
            this.Database = database;
        }

        public void Connect() {
            if (Connected) this.Close();

            Connection = new SqlConnection(ConnectionString);

            try {
                Console.WriteLine("Connecting to SQL...");
                Connection.Open();
                Connected = true;
            } catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
        }

        public void Close() {
            Console.WriteLine("Disconnecting from SQL...");
            Connection.Close();
            Connected = false;
        }

        public SqlCommand CreateCommand(string sql) {
            return new SqlCommand(sql, Connection);
        }
    }

    public class QuickField {
        public object Value { get; private set; }

        public string SQL { get; private set; }

        public SqlDbType DbType { get; private set; }

        public QuickField(object value, string SQL, SqlDbType DbType) {
            this.Value = value;
            this.SQL = SQL;
            this.DbType = DbType;
        }
    }
}